﻿nop
,,,BEGIn TEST
,,,
,,,

.L, Start,,
,,,
,,, Global Values Do not change
,,, 
,,,

,,,
,,, Intialize Game Setup Values/registers
,,, Can extract to use to test hardware
,,,
movi, $1, %ballx, pause button start game paused!
movi, $126, %bally
movi, $31, %p1_paddle
movi, $31, %p2_paddle
movi, $0, %p1_score
movi, $0, %p2_score
movi, $0, %p1_input
movi, $0, %p2_input


.L, BeginGame,, REPEATING FUNCTIONS BELOW: No variable preintializations that are dynamicly set

,,,
,,, Start Game/Pause Game
,,,

.L, pause
mov, %p1_input, %s3
mov, %p2_input, %s4 
movi, $1, %s2
lshi, $-8, %s2
and, %s2, %s3
and, %s2, %s4, set inputs to 1 or 0 if the start button not pushed
cmpi, $0, %s3
jei, &setpause
cmpi, $0, %s4
jei, &setpause
j, &gamepaused

.L, setpause
xori, $1, %ballx,, 0 is resume

.L, gamepaused
cmpi, $0, %ballx, start game
jei, &paddles
j, &pause

,,,
,,, Start button has been pushed
,,,

,,,
,,, Check restart
,,,

.L, restart
mov, %p1_input, %s3
mov, %p2_input, %s4 
movi, $1, %s2
lshi, $-9, %s2, select button
and, %s2, %s3
and, %s2, %s4, 
cmpi, $0, %s3
jei, &dntrestart
cmpi, $0, %s4
jei, &dntrestart
j, &Start

.L, dntrestart
,,,
,,,
,,,


.L, paddles
,,, Setup initial variables for paddle movement
,,,
,,, %s3 Lowerbound = 384
,,, Const: Upperbound = 0
,,, %s4 Speed = 2
,,,
,,,if(IsUp) --> !IsUpcollision --> T: MoveUp F: P1_End
,,,else if(isDown) --> !isDowncollision --> T: MoveDown F: P1_End
,,,else P1_END
 
,,,
,,, P1 paddle movement/collision
,,,

.L,    P1_input,,                  player 1 paddle
mov,   %p1_input,  %s1,
mov,   %s1,  %s2,
movi, $96, %s3,                    Put 96 into s3 to create 96<<2 = 384, lower bound of map
lshi,  $-2, %s3,                    96<<4 = 384 = s3
movi, $1, %S4,                     s4 = paddles speed must be in increments of 2 or equal to 1
 
,,,           
,,, Check for users input & collision
,,, 1) Masks input for up/down bit
,,, 2) Check upper/lower bound of map with current paddle position, if ok move paddle
,,, --My code flows sequentially instead of using jal/jr
 
 
,,,
,,, IsUp()
,,,
 
.L, P1_IsUp,,                  P1 IsUP()
movi, $1, %s6
lshi, $-7, %s6
and, %s6, %s1,             mask p1 input w/ up bit = s6
cmpi, $0, %s1,   		                 
jei, &P1_IsDown,,               if 0: its not set
 
,,,
,,, IsUpCollision(): IsUp() has been set by player 1: Check bounds
,,,
 
cmpi, $0,  %p1_paddle,     p1_paddle == upperbound of map
jei,          &P1_End,,    p1_paddle == upperbound else
nop
,,, MoveUp()
,,,
.L,   P1_MoveUp
sub,  %s4, %p1_paddle
j,    &P1_End,,            Finished moving up end p1 movement/collision detection cannot have more than 1 button
 
,,,
,,, IsDown()
,,,
 
.L, P1_IsDown,,
movi, $1, %s6
lshi, $-6, %s6                     
and, %s6, %s2,       mask p1 input w/ up bit
cmpi, $0, %s2,                      
jei,  &P1_End,,            if 0, its not set
 
,,,
,,, IsDownCollision: IsDown() has been set by player 1: Check bounds
,,,
 
cmp, %s3, %p1_paddle,     p1_paddle == lowerbound of map
jei,  &P1_End,,            p1_paddle == lowerbound do not move paddle
nop
,,, MoveDown()
,,,
.L,  P1_MoveDown
add, %s4, %p1_paddle
 
.L, P1_End
,,,
,,, End of P1 paddle movement
,,,

,,,
,,, p2 paddle
,,,

,,, Setup initial variables for paddle movement
,,,
,,, %s3 Lowerbound = 384
,,, Const: Upperbound = 0
,,, %s4 Speed = 2
,,,
,,,if(IsUp) --> !IsUpcollision --> T: MoveUp F: P1_End
,,,else if(isDown) --> !isDowncollision --> T: MoveDown F: P1_End
,,,else P1_END
 
,,,
,,, P2 paddle movement/collision
,,,

.L,    P2_input,,                  player 2 paddle
mov,   %p2_input,  %s1,
mov,   %s1,  %s2,
movi, $96, %s3,                    Put 96 into s3 to create 96<<2 = 384, lower bound of map
lshi,  $-2, %s3,                    96<<4 = 384 = s3
movi, $1, %S4,                     s4 = paddles speed must be in increments of 2 or equal to 1
 
,,,           
,,, Check for users input & collision
,,, 1) Masks input for up/down bit
,,, 2) Check upper/lower bound of map with current paddle position, if ok move paddle
,,, --My code flows sequentially instead of using jal/jr
 
 
,,,
,,, IsUp()
,,,
 
.L, P2_IsUp,,                  P2 IsUP()
movi, $1, %s6
lshi, $-7, %s6
and, %s6, %s1,             mask p2 input w/ up bit = s6
cmpi, $0, %s1,   		                 
jei, &P2_IsDown,,               if 0: its not set
 
,,,
,,, IsUpCollision(): IsUp() has been set by player 2: Check bounds
,,,
 
cmpi, $0,  %p2_paddle,     p2_paddle == upperbound of map
jei,          &P2_End,,    p2_paddle == upperbound else
nop
,,, MoveUp()
,,,
.L,   P2_MoveUp
sub,  %s4, %p2_paddle
j,    &P2_End,,            Finished moving up end p2 movement/collision detection cannot have more than 1 button
 
,,,
,,, IsDown()
,,,
 
.L, P2_IsDown,,
movi, $1, %s6
lshi, $-6, %s6                     
and, %s6, %s2,       mask p2 input w/ up bit
cmpi, $0, %s2,                      
jei,  &P2_End,,            if 0, its not set
 
,,,
,,, IsDownCollision: IsDown() has been set by player 1: Check bounds
,,,
 
cmp, %s3, %p2_paddle,     p2_paddle == lowerbound of map
jei,  &P2_End,,            p2_paddle == lowerbound do not move paddle
nop
,,, MoveDown()
,,,
.L,  P2_MoveDown
add, %s4, %p2_paddle
 
.L, P2_End
,,,
,,, End of P2 paddle movement
,,,

,,,
,,, Prevent game from going to fast, Refresh Rate Loop = 500
,,, s5 will equal 32766.... this will cause it to wait about 5 ms ish
,,,wait, $100

movi, $127, %s5
lshi, $-7, %s5
addi, $127, %s5
lshi, $-1, %s5

.L, wait
nop
nop
subi, $1, %s5
cmpi, $0, %s5
jei, &wait2Begin
j, &wait
,,,2x

.L, wait2Begin
movi, $127, %s5
lshi, $-3, %s5

.L, wait2
nop
nop
subi, $1, %s5
cmpi, $0, %s5
jei, &BeginGame
j, &wait2,, REPEAT MOVEMENT...